from direct.gui.DirectGui import DirectFrame, DirectButton, DirectLabel
from Cube.gui.QOptionMenu import QOptionMenu
from Cube.gui.QSizer import ResizedCallBackSet, BoxSizer
from Cube.gui.DirectGUIPropertiesAccess import AlignText
# default parameters
from DefaultParameters import DefaultParameter_BtnTeamName, DefaultParameter_LabelMemberSymbol, \
                              DefaultParameter_LabelMemberName, DefaultParameter_OptionMemberName, \
                              DefaultParameter_SizerTeams, DefaultParameter_SizerMembers
# update process
from UpdateProcess import UpdateProcess, LabelItemProcess
# btn's call back set
from Cube.gui.DirectGUIPropertiesAccess import BtnCmdSet

class MemberListControlFrame(DirectFrame, UpdateProcess):
    def __init__(self,
                 parent = None,
                 param_btnTeamName = {},
                 param_labelMemberSymbol = {},
                 param_labelMemberName = {},
                 param_optionMember = {},
                 param_sizerTeams = {},
                 param_sizerMembers = {},
                 memberListData = [],   # Cube.map.QMemberList.QMemberListEntry
                 connectType = "server",  # "server", "client"
                 memberTypeStringMap = None
                 ):
        DirectFrame.__init__(self, parent)
        UpdateProcess.__init__(self, memberTypeStringMap)

        # btn Team
        paramIn_btnTeam = DefaultParameter_BtnTeamName.copy()
        paramIn_btnTeam.update(param_btnTeamName)
        paramIn_btnTeam["parent"] = self
        # TODO...
        paramIn_labelMemberSymbol = DefaultParameter_LabelMemberSymbol.copy()
        paramIn_labelMemberSymbol.update(param_labelMemberSymbol)
        paramIn_labelMemberSymbol["parent"] = self
        # label Member
        paramIn_labelMember = DefaultParameter_LabelMemberName.copy()
        paramIn_labelMember.update(param_labelMemberName)
        paramIn_labelMember["parent"] = self
        # option Member
        paramIn_optionMember = DefaultParameter_OptionMemberName.copy()
        paramIn_optionMember.update(param_optionMember)
        paramIn_optionMember["parent"] = self

        paramIn_sizerTeams = DefaultParameter_SizerTeams.copy()
        paramIn_sizerTeams.update(param_sizerTeams)

        paramIn_sizerMembers = DefaultParameter_SizerMembers.copy()
        paramIn_sizerMembers.update(param_sizerMembers)

        # MemberList Layout
        teamGuiObjList = []
        memberGuiObjList = []
        currTeamID = None
        sizerTeams = BoxSizer(BoxSizer.VERTICAL, gap = paramIn_sizerTeams["gap"])
        for memberEntry in memberListData:
            # Cube.map.QMemberList.QMemberListEntry
            if memberEntry.TeamID != currTeamID:
                # add new team tag
                sizerMembers = BoxSizer(BoxSizer.VERTICAL, gap = paramIn_sizerMembers["gap"])
                paramIn_btnTeam["text"] = memberEntry.TeamName
                btn_TeamName = DirectButton(**paramIn_btnTeam)
                teamGuiObjList.append((btn_TeamName,memberEntry.TeamID))
                sizerMembers.Add(btn_TeamName,0)
                ResizedCallBackSet(btn_TeamName, AlignText, frame = btn_TeamName, align = "left" )
                sizerTeams.Add(sizerMembers,0)
                currTeamID = memberEntry.TeamID

            # add member tag
            # member symbol
            label_MemberSymbol = DirectLabel(**paramIn_labelMemberSymbol)
            # member name
            paramIn_labelMember["text"] = LabelItemProcess(memberEntry)
            label_MemberName = DirectLabel(**paramIn_labelMember)
            memberGuiObjList.append(label_MemberName)

            sizerMemberInfo = BoxSizer(BoxSizer.HORIZONTAL)
            sizerMemberInfo.Add(label_MemberSymbol,1)
            sizerMemberInfo.Add(label_MemberName,6)
            ResizedCallBackSet(label_MemberName, AlignText, frame = label_MemberName, align = "left" )
            sizerMembers.Add(sizerMemberInfo,0)

        sizerTeams.Attach2Frame(self)

        # save parameter and gui objects
        self.paramIn_labelMember = paramIn_labelMember
        self.paramIn_optionMember = paramIn_optionMember
        self.connectType = connectType
        self.MemberListControlItemList = memberGuiObjList
        self.TeamBtnitemList = teamGuiObjList

    def TeamBtnCallBackSet(self, callBackFn):
        for (btn,i) in self.TeamBtnitemList:
            BtnCmdSet(btn, callBackFn, [i])
